package com.cater.talents.controller;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.talents.entity.CellPhone;
import com.cater.talents.service.CellPhoneServiceImpl;

/**
 * @author yong
 * @Date 2018年3月22日-下午9:54:50
 * @手机号码管理控制层
 */
@Controller
@RequestMapping("/cellphone")
public class CellPhoneController {

	@Autowired
	private CellPhoneServiceImpl service;

	/**
	 * @手机号码管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "talents/cellphone/list";
	}

	/**
	 * @手机号码管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询手机号码分页数据")
	public PageResult<CellPhone> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<CellPhone> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("cellphone:add")
	public String addView(Model model, CommonQuery query) {
		return "talents/cellphone/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("cellphone:add")
	@ResponseBody
	@SystemControllerLog(description = "添加手机号码")
	public JSONObject add(CellPhone t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		query.setName(t.getCellphone());
		int count = service.getByCellphone(query);
		if (count > 0) {
			json.put("code", 999);
			json.put("message", "电话号码已存在");
		} else {
			if (StringUtils.isNotBlank(id)) {
				json.put("code", 200);
				json.put("message", "success");
			} else {
				json.put("code", 999);
				json.put("message", "fail");
			}
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("cellphone:edit")
	public String editView(String id, Model model, CommonQuery query) {
		CellPhone t = service.getById(id);
		model.addAttribute("cellphones", t);
		return "talents/cellphone/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("cellphone:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改手机号码")
	public JSONObject update(CellPhone t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			CellPhone cellPhone = service.getById(t.getId());
			int count = 0;
			if (!t.getCellphone().equals(cellPhone.getCellphone())) {
				query.setName(t.getCellphone());
				count = service.getByCellphone(query);
				if (count > 0) {
					json.put("code", 999);
					json.put("message", "标签名已存在");
				}
			} else if (count == 0) {
				service.update(t);
				json.put("code", 200);
				json.put("message", "修改成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("cellphone:delete")
	@SystemControllerLog(description = "删除手机号码")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	@RequestMapping("addphone")
	public String AddPhone(Model model, HttpServletRequest request) {
		String talentid = request.getParameter("talentid");
		model.addAttribute("tempids", UUID.randomUUID().toString().replaceAll("-", ""));
		model.addAttribute("talentid", talentid);
		return "talents/cellphone/add_phone";
	}
}
